

project , uses(../data_output/legislators_klarner.dta)
	
		
	**********
	** OKLAHOMA House	
	** Life-time term limites: 6 x 2-year terms.
	** First year: 2004	
	**********
	
	use ../data_output/legislators_klarner.dta, clear
	keep if state=="OK" & chamber=="house"
	keep if regexm(term, "1993|1995|1997|1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = trim(regexs(1)) if regexm(lower(CandName),"([^,]+)")
	gen firstname = trim(regexs(1)) if regexm(lower(CandName),",( [^ ]+)")
	gen mergename = lastname
	bys lastname term: gen n = _n
	by lastname term: egen mean_n=mean(n)
	replace mergename =  lastname + " (" + firstname + ")" if mean_n>1
	drop n mean_n
	save ../data_output/legislators_OK_house_1993_2016.dta, replace
	
	gen e_won = elections_won
	qui forval i= 1960(2)1990{ //years enacted
		replace e_won = subinstr(e_won, "`i';", "",.)
	}
	tostring year, gen(regexp)
	replace regexp = "^([^!]+"+regexp+";)"
	gen previous_wins = regexs(1) if regexm(e_won, regexp)
	egen prev_terms = noccur(previous_wins) , string(";")
	gen termlimited=prev_terms==6	
	save ../data_output/legislators_OK_house_1993_2016.dta, replace
project, creates(../data_output/legislators_OK_house_1993_2016.dta)	

	
	
	//bills
	
	
	clear
	gen name = ""
	tempfile tmp
	save `tmp', replace
	local files : dir "../data_input/OK/" files "ok_bills_*.csv"
	qui foreach file in `files' {
project, original(../data_input/OK/`file')
		import delimited using ../data_input/OK/`file',  clear  delimiters(";") bindquotes(strict) encoding("utf-8")		
		append using `tmp'
		save `tmp', replace
	}
	
	gen chamber = ""
	replace chamber = "house" if regexm( lower(name) , "representativ|[(]h[)]")
	replace chamber = "senate" if regexm( lower(name) , "senat|[(]s[)]")
	replace type = "sponsor" if regexm(lower(name), "principal")
	foreach i in "principal" "add as" "senator" "representative(s)" "representative" "coauthor" "author" "senate" "(  )" "(h)" "(s)"{
		replace name = subinstr( lower(name) , "`i'","",.)
	}
	replace name = trim(name)
	
	keep if chamber=="house"
	gen mergename = name
	replace mergename = subinstr(mergename, "'", "",.)
	replace mergename = trim(mergename)
	tostring year, generate(inoffice)
	gen term = ""
	replace term = "1991-1992" if regexm(inoffice, "1991|1992")
	replace term = "1993-1994" if regexm(inoffice, "1993|1994")
	replace term = "1995-1996" if regexm(inoffice, "1995|1996")	
	replace term = "1997-1998" if regexm(inoffice, "1997|1998")
	replace term = "1999-2000" if regexm(inoffice, "1999|2000")
	replace term = "2001-2002" if regexm(inoffice, "2001|2002")
	replace term = "2003-2004" if regexm(inoffice, "2003|2004")
	replace term = "2005-2006" if regexm(inoffice, "2005|2006")
	replace term = "2007-2008" if regexm(inoffice, "2007|2008")
	replace term = "2009-2010" if regexm(inoffice, "2009|2010")
	replace term = "2011-2012" if regexm(inoffice, "2011|2012")
	replace term = "2013-2014" if regexm(inoffice, "2013|2014")
	replace term = "2015-2016" if regexm(inoffice, "2015|2016")
	replace mergename = "maddox" if mergename == "maddox (jim)" & regexm(term, "1993")
	replace mergename = "maddux" if mergename == "maddux (elmer)" & regexm(term, "1993")
	replace mergename = "rhoads" if mergename == "rhoads (karroll)" & regexm(term, "1993")
	replace mergename = "mcdaniel (john)" if mergename == "mcdaniel (randy)" & regexm(term, "2007|2009|2011|2013|2015")	
	replace mergename = "bryant (james)" if mergename == "bryant (james sears)" & regexm(term, "1993")
	replace mergename = "pope" if mergename == "pope (tim)" & regexm(term, "1993")
	replace mergename = "rhodes" if mergename == "rhodes (dusty)" & regexm(term, "1993")
	replace mergename = "stites" if mergename == "stites (j.t.)" & regexm(term, "1993|1995|1997|1999|2001")
	replace mergename = "miller" if mergename == "miller (doug)" & regexm(term, "1995|1997")
	replace mergename = "morgan" if mergename == "morgan (fred)" & regexm(term, "1995|1997|1999|2001")
	replace mergename = "peterson" if mergename == "peterson (ron)" & regexm(term, "2001|2003")
	replace mergename = "wright" if mergename == "wright (john)" & regexm(term, "1999|2001|2003|2005|2007")
	replace mergename = "mcdaniel" if mergename == "mcdaniel (jeannie)" & regexm(term, "2005")
	replace mergename = "martin" if mergename == "martin (steve)" & regexm(term, "2005")
	replace mergename = "johnson" if mergename == "johnson (rob)" & regexm(term, "2005")
	replace mergename = "coody" if mergename == "coody (ann)" & regexm(term, "2005|2007|2009|2011|2013")
	replace mergename = "osborn" if mergename == "osborn (leslie)" & regexm(term, "2009|2011|2013")
	replace mergename = "bennett" if mergename == "bennett (john)" & regexm(term, "2013|2015")
	
	merge m:1  mergename term  using ../data_output/legislators_OK_house_1993_2016.dta
	
	*3 obs are not merging: abdo, auffet, key
	drop if _merge==1
	gen law = regexm(lower(status), "approved by governor|enrolled|filed with secretary of state") & type=="sponsor"
	gen sponsor = 1 if bill!="" & type=="sponsor"
	gen cosponsor = 1 if bill!="" & type=="cosponsor"
	gen sponsor_approp = regexm( lower(history) , "referred to approp") & sponsor==1
	gen billlength = length(billtext)	 & sponsor==1

	save ../data_output/bills_by_sponsor_OK_1993_2016.dta, replace
project, creates(../data_output/bills_by_sponsor_OK_1993_2016.dta)	
	
	use ../data_output/bills_by_sponsor_OK_1993_2016.dta, clear
	collapse (sum) cosponsor sponsor* law billlength (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )
	
	save ../data_output/total_bills_by_sponsor_OK_1993_2016.dta, replace
project, creates(../data_output/total_bills_by_sponsor_OK_1993_2016.dta)		
		
	

	**Voting
	
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(CandName),"[^,]+, ([^ ])")
	keep if state=="OK" & chamber=="house"
	keep if regexm(term, "1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = lastname
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = lastname + ", " + firstname+"."  if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = CandName if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	tempfile tmp
	save `tmp', replace
	
	
project, original(../data_input/OK/ok_votes.csv)	
	import delimited using ../data_input/OK/ok_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	keep if chamber=="house"
	tostring session, replace
	gen term = ""
	replace term = "1999-2000" if session=="47"
	replace term = "2001-2002" if session=="48"
	replace term = "2003-2004" if session=="49"
	replace term = "2005-2006" if session=="50"
	replace term = "2007-2008" if session=="51"
	replace term = "2009-2010" if session=="52"
	replace term = "2011-2012" if session=="53"
	replace term = "2013-2014" if session=="54"
	replace term = "2015-2016" if session=="55"
	gen os_mergename = trim(lower(name))
	replace os_mergename = lower(os_mergename)
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)	
	replace os_mergename = "hiett"	if os_mergename=="mr. speaker" & regexm(term, "2005")
	replace os_mergename = "steele"	if os_mergename=="mr.speaker" & regexm(term, "2011")

	replace os_mergename = "MARTIN, SCOTT C."	if os_mergename=="martin, sc" & regexm(term, "2007|2009|2011")
	replace os_mergename = "MARTIN, SCOTT C."	if os_mergename=="martin, sc." & regexm(term, "2011|2013")
	replace os_mergename = "MARTIN, STEVE"	if os_mergename=="martin, st." & regexm(term, "2007|2009|2011|2013")
	replace os_mergename = "MCDANIEL, JEANNIE"	if os_mergename=="mcdaniel, j." & regexm(term, "2007|2009|2011|2013|2015")
	replace os_mergename = "MCDANIEL, JOHN RANDALL (RANDY)"	if os_mergename=="mcdaniel, r." & regexm(term, "2007|2009|2011|2013|2015")
	
	merge m:1  os_mergename term using `tmp'		
	
	// 3 obs not merging
	drop if _merge==1
	drop _merge
	gen floorvotes = regexm(vote, "yes|no")
	replace vote = "1" if vote=="yes"
	replace vote = "-1" if vote=="no"
	replace vote = "0" if regexm(vote, "1")==0
	destring vote, replace
	egen vote_id= group(session chamber location motion date bill)
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	bys vote_id: egen votemargin = mean(vote)
	gen close_floorvotes = floorvotes if abs(votemargin)<.1			

	save ../data_output/floorvotes_OK_1999_2016.dta, replace	
project, creates(../data_output/floorvotes_OK_1999_2016.dta)	
	
	use ../data_output/floorvotes_OK_1999_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum) *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	
	save ../data_output/total_floorvotes_OK_1999_2016.dta, replace
project, creates(../data_output/total_floorvotes_OK_1999_2016.dta)	
	
	
